/**
 * <b>项目名：</b>hclv-web<br/>
 * <b>包名：</b>com.flea.hclv.controller<br/>
 * <b>文件名：</b>BaseAPIController.java<br/>
 * <b>版本信息：</b>1.0<br/>
 * <b>日期：</b>2018年9月15日 下午9:23:02<br/>
 * <b>COPYRIGHT 2010-2012 ALL RIGHTS RESERVED 虫子科技</b>-版权所有<br/>
 */
package com.flea.hclv.controller;

import com.flea.hclv.constant.HConstant;
import com.flea.hclv.util.HclvDictUtil;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.base.util.StringUtil;
import com.flea.system.entity.OrgDept;
import com.flea.system.entity.OrgUser;
import com.flea.system.entity.WebDict;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.ControllerResult;
import com.flea.system.utils.FleaStringUtils;
import com.google.common.collect.Lists;
import flea.sys.cache.FleaCacheUtil;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description
 * @createTime 2018年9月15日 下午9:23:02
 * @modifyTime
 * @author myflea@163.com
 * @version 1.0
 */
@Controller
@RequestMapping("/flea/hclv/")
public class BaseAPIController {
    private Logger logger = LoggerFactory.getLogger(BaseAPIController.class);

    @SuppressWarnings("unchecked")
    @RequestMapping("getProvinces")
    public void getProvinces(HttpServletResponse response) {

        Object provicesIncache = FleaCacheUtil.get("provinces");
        List<WebDict> provices = null;
        if (null != provicesIncache) {
            logger.info("在缓存中获取province信息，province信息不为空");
            provices = (List<WebDict>) provicesIncache;
        }
        if (null == provices || provices.size() == 0) {

            provices = HclvDictUtil.getProvinces();
            FleaCacheUtil.put("provinces", provices);
            logger.info("在数据库服务中获取province信息，province信息不为空,并且将缓存数据写入缓存中");
        }


        ControllerResult cResult = new ControllerResult(true, "操作成功", provices);
        cResult.WriteToPage(response);
    }

    @SuppressWarnings("unchecked")
    @RequestMapping("getCity")
    public void getCity(HttpServletRequest request, HttpServletResponse response) {
        String proviceCode = request.getParameter("pcode");
        List<WebDict> city;
        Object citysInCache = FleaCacheUtil.get(proviceCode);
        if (null != citysInCache) {
            logger.debug("根据省份获取城市信息，在cache中存在，cache不为空");
            city = (List<WebDict>) citysInCache;
        } else {
            city = HclvDictUtil.getCity(proviceCode);
            FleaCacheUtil.put(proviceCode, city);
            logger.debug("根据省份获取城市信息，在cache中不存在，在DB中获取，别写入缓存");
        }

        ControllerResult cResult = new ControllerResult(true, "操作成功", city);
        cResult.WriteToPage(response);
    }

    @SuppressWarnings("unchecked")
    @RequestMapping("getZone")
    public void getZone(HttpServletRequest request, HttpServletResponse response) {
        String cityCode = request.getParameter("pcode");
        Object o = FleaCacheUtil.get(cityCode);
        List<WebDict> zones;
        if (o == null) {

            zones = HclvDictUtil.getZones(cityCode);
            FleaCacheUtil.put(cityCode, zones);
            logger.debug("根据城市获取地区信息，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            zones = (List<WebDict>) o;
            logger.debug("根据城市获取地区信息，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", zones);
        cResult.WriteToPage(response);
    }

    @RequestMapping("getUUID")
    public void getUUID(HttpServletResponse response) {
        ControllerResult cResult = new ControllerResult(true, "操作成功", FleaStringUtils.getGenerateUUID());
        cResult.WriteToPage(response);
    }

    //旅游类别
    @SuppressWarnings("unchecked")
    @RequestMapping("getTourism")
    public void getTourism(HttpServletResponse response) {
        String key = "tourismType";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> zones;
        if (o == null) {
            zones = HclvDictUtil.getTourism();
            FleaCacheUtil.put(key, zones);
            logger.debug("获取旅游类别信息，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            zones = (List<WebDict>) o;
            logger.debug("获取旅游类别信息，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", zones);
        cResult.WriteToPage(response);
    }

    //行程类型
    @SuppressWarnings("unchecked")
    @RequestMapping("getTrip")
    public void getTrip(HttpServletResponse response) {
        String key = "tripType";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> zones;
        if (o == null) {
            zones = HclvDictUtil.getTrip();
            FleaCacheUtil.put(key, zones);
            logger.debug("获取行程类型信息，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            zones = (List<WebDict>) o;
            logger.debug("获取行程类型信息，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", zones);
        cResult.WriteToPage(response);
    }

    //说明表 类型
    @SuppressWarnings("unchecked")
    @RequestMapping("getExplain")
    public void getExplain(HttpServletResponse response) {
        String key = "explainType";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> zones;
        if (o == null) {
            zones = HclvDictUtil.getExplain();
            FleaCacheUtil.put(key, zones);
            logger.debug("获取说明表类型信息，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            zones = (List<WebDict>) o;
            logger.debug("获取说明表类型信息，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", zones);
        cResult.WriteToPage(response);
    }

    @RequestMapping("getVehicle")
    public void getVehicle(HttpServletResponse response) {
        List<WebDict> provices = HclvDictUtil.getVehicle();
        ControllerResult cResult = new ControllerResult(true, "操作成功", provices);
        cResult.WriteToPage(response);
    }

    @RequestMapping("getDrivingModel")
    public void getDrivingModel(HttpServletResponse response) {
        List<WebDict> provices = HclvDictUtil.getDrivingModel();
        ControllerResult cResult = new ControllerResult(true, "操作成功", provices);
        cResult.WriteToPage(response);
    }

    // 证件类型
    @SuppressWarnings("unchecked")
    @RequestMapping("getCertificates")
    public void getCertificates( HttpServletResponse response) {
        String key = "certificates";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> data;
        if (o == null) {
            data = HclvDictUtil.getCertificates();
            FleaCacheUtil.put(key, data);
            logger.debug("获取证件类型信息，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            data = (List<WebDict>) o;
            logger.debug("获取证件类型信息，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", data);
        cResult.WriteToPage(response);
    }

    // 产品销售状态
    @SuppressWarnings("unchecked")
    @RequestMapping("getSaleStatus")
    public void getSaleStatus(HttpServletResponse response) {
        String key = "saleStatus";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> data;
        if (o == null) {
            data = HclvDictUtil.getProductSaleStatus();
            FleaCacheUtil.put(key, data);
            logger.debug("获取产品状态信息，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            data = (List<WebDict>) o;
            logger.debug("获取产品状态信息，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", data);
        cResult.WriteToPage(response);
    }

    // 支付类型
    @SuppressWarnings("unchecked")
    @RequestMapping("getPayType")
    public void getPayType(HttpServletResponse response) {
        String key = "payTypeStatus";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> data;
        if (o == null) {
            data = HclvDictUtil.getPayType();
            FleaCacheUtil.put(key, data);
            logger.debug("获取支付类型信息，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            data = (List<WebDict>) o;
            logger.debug("获取支付类型信息，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", data);
        cResult.WriteToPage(response);
    }

    // 支付类型2
    @SuppressWarnings("unchecked")
    @RequestMapping("getPayTypeTwo")
    public void getPayTypeTwo(HttpServletResponse response) {
        String key = "payTypeStatusTwo";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> data;
        List<WebDict> newData = Lists.newArrayList();
        if (o == null) {
            data = HclvDictUtil.getPayType();
            if (CollectionUtils.isNotEmpty(data)){
                for (WebDict webDict : data){
                    String code= webDict.getDictCode();
                    if (!code.equals(HConstant.D_PAY_TYPE_CASH)){
                        newData.add(webDict);
                    }
                }
            }

            FleaCacheUtil.put(key, newData);
            logger.debug("获取支付类型信息，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            newData = (List<WebDict>) o;
            logger.debug("获取支付类型信息，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", newData);
        cResult.WriteToPage(response);
    }

    // 游客类型
    @SuppressWarnings("unchecked")
    @RequestMapping("getVisitorType")
    public void getVisitorType(HttpServletResponse response) {
        String key = "visitorType";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> data;
        if (o == null) {
            data = HclvDictUtil.getVisitorType();
            FleaCacheUtil.put(key, data);
            logger.debug("获取游客类型信息，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            data = (List<WebDict>) o;
            logger.debug("获取游客类型信息，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", data);
        cResult.WriteToPage(response);
    }

    // 旅游团状态
    @SuppressWarnings("unchecked")
    @RequestMapping("getTravelGroupStatus")
    public void getTravelGroupStatus(HttpServletResponse response) {
        String key = "travelGroupStatus";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> data;
        if (o == null) {
            data = HclvDictUtil.getTravelGroupStatus();
            FleaCacheUtil.put(key, data);
            logger.debug("获取旅游团状态信息，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            data = (List<WebDict>) o;
            logger.debug("获取旅游团状态信息，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", data);
        cResult.WriteToPage(response);
    }

    //获取游客订单状态
    @SuppressWarnings("unchecked")
    @RequestMapping("getVisitorOrderStatus")
    public void getVisitorOderStatus(HttpServletResponse response) {
        String key = "visitorOrderStatus";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> data;
        if (o == null) {
            data = HclvDictUtil.getVisitorOderStatus();
            FleaCacheUtil.put(key, data);
            logger.debug("获取游客订单状态，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            data = (List<WebDict>) o;
            logger.debug("获取游客订单状态，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", data);
        cResult.WriteToPage(response);
    }

    //客户缴费状态
    @SuppressWarnings("unchecked")
    @RequestMapping("getCustomerStatus")
    public void getCustomerStatus(HttpServletResponse response) {
        String key = "customerPayStatus";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> data;
        if (o == null) {
            data = HclvDictUtil.getCustomerStatus();
            FleaCacheUtil.put(key, data);
            logger.debug("获取客户缴费状态，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            data = (List<WebDict>) o;
            logger.debug("获取客户缴费状态，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", data);
        cResult.WriteToPage(response);
    }

    //供应商与组团社之间订单的状态
    @SuppressWarnings("unchecked")
    @RequestMapping("getServiceOrgderStatus")
    public void getServiceOrgderStatus(HttpServletResponse response) {
        String key = "serviceOrgderStatus";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> data;
        if (o == null) {
            data = HclvDictUtil.getServiceOrgderStatus();
            FleaCacheUtil.put(key, data);
            logger.debug("获取服务订单状态，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            data = (List<WebDict>) o;
            logger.debug("获取服务订单状态，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", data);
        cResult.WriteToPage(response);
    }

    //服务组织之间订单修改申请状态
    @SuppressWarnings("unchecked")
    @RequestMapping("getUpdateSerivcOrderUpdateApplyStatus")
    public void getUpdateSerivcOrderUpdateApplyStatus(HttpServletResponse response) {
        String key = "updateApplyStatus";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> data;
        if (o == null) {
            data = HclvDictUtil.getUpdateSerivcOrderUpdateApplyStatus();
            FleaCacheUtil.put(key, data);
            logger.debug("获取服务订单修改申请状态，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            data = (List<WebDict>) o;
            logger.debug("获取服务订单修改申请状态，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", data);
        cResult.WriteToPage(response);
    }

    //D_AUDIT_STATUS
    //导游审核状态
    @SuppressWarnings("unchecked")
    @RequestMapping("getGuideAuditStatus")
    public void getGuideAuditStatus(HttpServletResponse response) {
        String key = "guideAuditStatus";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> data;
        if (o == null) {
            data = HclvDictUtil.getGuideAuditStatus();
            FleaCacheUtil.put(key, data);
            logger.debug("获取导游审核状态，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            data = (List<WebDict>) o;
            logger.debug("获取导游审核状态，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", data);
        cResult.WriteToPage(response);
    }

    //导游团支付状态
    @SuppressWarnings("unchecked")
    @RequestMapping("getGuidePayStatus")
    public void getGuidePayStatus(HttpServletResponse response) {
        String key = "guidePayStatus";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> data;
        if (o == null) {
            data = HclvDictUtil.getGuidePayStatus();
            FleaCacheUtil.put(key, data);
            logger.debug("获取导游团支付状态，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            data = (List<WebDict>) o;
            logger.debug("获取导游团支付状态，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", data);
        cResult.WriteToPage(response);
    }
    //预定车状态
    @SuppressWarnings("unchecked")
    @RequestMapping("getCarReserveStatus")
    public void getCarReserveStatus(HttpServletResponse response) {
        String key = "getCarReserveStatus";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> data;
        if (o == null) {
            data = HclvDictUtil.getCarReserveStatus();
            FleaCacheUtil.put(key, data);
//            logger.debug("获取导游团支付状态，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            data = (List<WebDict>) o;
//            logger.debug("获取导游团支付状态，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", data);
        cResult.WriteToPage(response);
    }
    //预定车支付状态
    @SuppressWarnings("unchecked")
    @RequestMapping("getCarReservePayStatus")
    public void getCarReservePayStatus(HttpServletResponse response) {
        String key = "getCarReservePayStatus";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> data;
        if (o == null) {
            data = HclvDictUtil.getCarReservePayStatus();
            FleaCacheUtil.put(key, data);
//            logger.debug("获取导游团支付状态，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            data = (List<WebDict>) o;
//            logger.debug("获取导游团支付状态，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", data);
        cResult.WriteToPage(response);
    }
    //预定车修改申请状态
    @SuppressWarnings("unchecked")
    @RequestMapping("getCarReserveApplyStatus")
    public void getCarReserveApplyStatus(HttpServletResponse response) {
        String key = "getCarReserveApplyStatus";
        Object o = FleaCacheUtil.get(key);
        List<WebDict> data;
        if (o == null) {
            data = HclvDictUtil.getCarReserveApplyStatus();
            FleaCacheUtil.put(key, data);
//            logger.debug("获取导游团支付状态，在cache中不存在，在DB中获取，并且写入缓存");
        } else {
            data = (List<WebDict>) o;
//            logger.debug("获取导游团支付状态，在cache中存在，cache不为空");
        }
        ControllerResult cResult = new ControllerResult(true, "操作成功", data);
        cResult.WriteToPage(response);
    }

    //test
//    @Autowired
//    private HclvSysLogDao hclvSysLogDaoImpl;

//    @ResponseBody
//    @RequestMapping("test")
//    public ServerResponse test(HttpServletResponse response) {
//        HclvSysLog log = new HclvSysLog();
////      log.setMessage("----------------------zhigfafdasdfadsfadsfadsfsdfasdfa");
////      List<HclvSysLog> result = hclvSysLogDaoImpl.findExceptionInfoPaging(1,20);
//        List<HclvSysLog> result = hclvSysLogDaoImpl.findExceptionInfo();
//        return ServerResponse.createBySuccess(result);
//
//    }

    /**
     * 刷新缓存信息
     */
    @ResponseBody
    @RequestMapping("clearCache")
    public ServerResponse clearMemcache() {
        FleaCacheUtil.clear(HConstant.FLEAL_CACHE_POOL_NAME);
        return ServerResponse.createBySuccess("刷新缓存");
    }

    /**
     * 获取当前用户所在的组织机构ID
     */
    @ResponseBody
    @RequestMapping("getCurrentOrgUUid")
    public ServerResponse getCurrentOrgUUid(HttpServletRequest request) {
        String curOrgUuid = FleaOrgUtils.getCurrentUserDeptUuid(request);
        return ServerResponse.createBySuccess(curOrgUuid);
    }

    @ResponseBody
    @RequestMapping("getCurrentOrgType")
    public ServerResponse getCurrentOrgType(HttpServletRequest request) {
        OrgDept orgDept = FleaOrgUtils.getCurrentUserDept(request);
        if (orgDept == null) {
            return ServerResponse.createByErrorMessage("部门不存在");
        }
        String orgServiceType = orgDept.getServiceType();

        String type = null;
        WebDict reuslt = null;
        List<WebDict> result = HclvDictUtil.getServiceOrgType();
        for (WebDict wd : result) {
            if (orgServiceType.equals(wd.getDictCode())) {
                reuslt = wd;
                break;
            }

        }
        return ServerResponse.createBySuccess(reuslt);
    }

    @ResponseBody
    @RequestMapping("getServiceOrgType")
    public ServerResponse getServiceOrgType() {
        List<WebDict> result = HclvDictUtil.getServiceOrgType();
        return ServerResponse.createBySuccess(result);
    }
    //只获取供应商类型和组团社类型
    @ResponseBody
    @RequestMapping("getServiceOrgTypeTwo")
    public ServerResponse getServiceOrgTypeTwo() {
        List<WebDict> result = HclvDictUtil.getServiceOrgType();
        List<WebDict> newData = Lists.newArrayList();
        if (result!=null){
            for (WebDict webDict : result){
                String dict = webDict.getDictCode();
                if (dict.equals(HConstant.D_SERVICE_ORG_SUPPLIER) || dict.equals(HConstant.D_SERVICE_ORG_GROUP)){
                    newData.add(webDict);
                }
            }
        }
        return ServerResponse.createBySuccess(newData);
    }

    /**
     * 服务组织订单修改类型
     */
    @ResponseBody
    @RequestMapping("getServiceApplyType")
    public ServerResponse getServiceApplyType() {
        List<WebDict> result = HclvDictUtil.getServiceApplyType();
        return ServerResponse.createBySuccess(result);
    }
    /**
     * 地铁项目列表
     */
    @ResponseBody
    @RequestMapping("getDictHTProject")
    public ServerResponse getDictHTProject() {
    	List<WebDict> result = HclvDictUtil.getHTProjectList();
    	return ServerResponse.createBySuccess(result);
    }
    /**
     * 工种类别
     */
    @ResponseBody
    @RequestMapping("getDictWorkType")
    public ServerResponse getDictWorkType() {
    	List<WebDict> result = HclvDictUtil.getDictWorkType();
    	return ServerResponse.createBySuccess(result);
    }
    /**
     * 人脸识别系统角色
     */
    @ResponseBody
    @RequestMapping("getFaceRoleType")
    public ServerResponse getFaceRoleType() {
    	List<WebDict> result = HclvDictUtil.getFaceRoleType();
    	return ServerResponse.createBySuccess(result);
    }
    /**
     * 获取队伍列表
     */
    @ResponseBody
    @RequestMapping("getTeams")
    public ServerResponse getTeams() {
    	List<WebDict> result = HclvDictUtil.getTeams();
    	return ServerResponse.createBySuccess(result);
    }
    @ResponseBody
    @RequestMapping("getUserBD")
    public ServerResponse getUserBD(HttpServletRequest request) {
    	OrgUser orgUser = 	FleaOrgUtils.getCurUser(request);
    	String bdType = orgUser.getExtend1();
    	if(StringUtil.isEmpty(bdType)) {
    		return ServerResponse.createByErrorCodeMassage(500, "当前用户没有设置标段信息，非法");
    	}
		return ServerResponse.createBySuccess(bdType);
    }


}